﻿package Leophy.Management {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.text.TextFormat;

	import Leophy.Interfaces.IAsynCallBack;
	import Leophy.Interfaces.IResource;
	import Leophy.Interfaces.IResourceManager;
	import Leophy.Interfaces.ISay;
	import Leophy.LeophyVersion;
	import Leophy.Net.XMLLoader;
	import Leophy.text.Language;
	import Leophy.text.MyFont;
	import Leophy.text.MyTextField;
	import Leophy.text.MyTextFormat;

	/**
	 * ...
	 * @author TOPHY
	 */
	public class ResourceManager extends EventDispatcher implements ISay, IResourceManager {

		public static var M_RSLOADED:String = "onResourceLoaded";
		
		
		private static var _instance : ResourceManager;
		

		/**
		 * 单例
		 */
		public static function get Ins() : ResourceManager {
			if (_instance == null) {
				_instance = new ResourceManager();
			}
			return _instance;
		}

		private var sourceloader : XMLLoader;
		private var SourceList : Object;
		private var SourceDataList : Object;

		public function ResourceManager(sourceXmlUrl : String = null) {
			LeophyVersion.version();
			SourceList = new Object();
			SourceDataList = new Object();
			sourceloader = new XMLLoader();
			sourceloader.addEventListener(XMLLoader.COMPLETE_XML, loadedresource);
			sourceloader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorOnLoadRSFile);
			sourceloader.addEventListener(ProgressEvent.PROGRESS, onloading);
			
			if (sourceXmlUrl != null || sourceXmlUrl != "") {
				loadSource(sourceXmlUrl);
			}
		}

		/**
		 * 加载资源
		 * @param	sourceXmlUrl 资源文件路径
		 */
		public function loadSource(sourceXmlUrl : String) : void {
			if (sourceXmlUrl == "" || sourceXmlUrl == null) {
				return;
			}
			var urlreqesue : URLRequest = new URLRequest(sourceXmlUrl);
			sourceloader.load(urlreqesue);
		}

		/**
		 * 注册资源
		 * @param	source
		 * @param	id
		 * @return
		 */
		public function RegistResource(source:IResource) : Boolean {
			
			if (SourceList[source.getSourceId()] == null) {
				SourceList[source.getSourceId()] = new Array();
				//say("已经有相同的SourceId注册！");
				//return false;
			}
			var array:Array=SourceList[source.getSourceId()] as Array
			array.push(source);
			Language.Ins.addEventListener(Language.M_LANGUAGE_CHANGED, source.onLanguageChange);
			
			source.onLanguageChange(null);
			//BeginSetStyle(source, Language.Ins.CurrentLanguage);
			return true;
		}

		/**
		 * 异步设置文本样式
		 * @param	id 资源id
		 * @param	lang 语言版本
		 * @return
		 */
		public function BeginSetStyle(source:Object, lang : String) : Boolean {
			
			if (source == null || lang == null || lang == "") return false;
		
			var smap : ResourceData = this.SourceDataList[IResource(source).getSourceId()];
			var rs : IAsynCallBack = source as IAsynCallBack;
			
			if (smap == null || rs == null) return false;
			
			var textf : TextFormat = MyTextFormat.Ins.getTextFormat(smap.GetCSSByLang(lang));
			
			/*  define an new textFormat for resource text which not set
			if (textf == null) {
			textf = new TextFormat();
			}
			 */
			if (textf != null) {
				MyFont.Ins.BeginEmbedFont(textf.font, "setstyle", rs, textf);
			}
			return true;
		}

		/**
		 * 处理资源对应表
		 * @param	xmll
		 */
		private function ProcessLanguage_CSS_Map(xmll : XMLList) : void {
			
			for (var i : uint = 0;i < xmll.length();i++) {
				var ssid : String = xmll[i].@RSID.toString();
				if (SourceDataList[ssid] == null) {
					SourceDataList[ssid] = new ResourceData(ssid);
				}
				ResourceData(SourceDataList[ssid]).InitLangCss(xmll[i]);
			}
		}

		private function onloading(ev : ProgressEvent) : void {
			this.dispatchEvent(ev);
		}

		private function IOErrorOnLoadRSFile(ev : IOErrorEvent) : void {
			ev.text = "IOERROR on Loading sourcefile.\r" + ev.text;
			this.dispatchEvent(ev);
		}

		private function loadedresource(ev : Event) : void {
			//trace(ev.target.data);
			Language.Ins.loadLGResource(ev.target.data.languageRS_Url.toString());
			Language.Ins.addEventListener(Language.M_LOADED_LANGUAGE, rs_loaded);
			
			MyTextFormat.Ins.loadCSS(ev.target.data.Css_Url.toString());
			MyTextFormat.Ins.addEventListener(Event.COMPLETE, rs_loaded);
			
			MyFont.Ins.loadFontList(ev.target.data.fonts_Url.toString(), ev.target.data.fontsIndex_Url.toString());
			MyFont.Ins.addEventListener(Event.COMPLETE, rs_loaded);
			
			// 处理样式映射表
			ProcessLanguage_CSS_Map(ev.target.data.ResourceStyle.map);
			
			say("Resource loaded!");
		}		

		private function rs_loaded(ev:*) {
			//trace(Language.Ins.Languageloaded, MyTextFormat.Ins.CssLoaded, MyFont.Ins.isFontListLoaded);
			if (Language.Ins.Languageloaded && MyTextFormat.Ins.CssLoaded && MyFont.Ins.isFontListLoaded) {
				this.dispatchEvent(new Event(ResourceManager.M_RSLOADED));
			}
		}
		
		
		/* INTERFACE Leophy.Interfaces.ISay */
		public function say(o : *) : void {
			trace("RMr:", o.toString());
		}
	}
}

